package com.jr.controller;

import com.google.gson.Gson;
import com.jr.pojo.AddSupplierWhiteList;
import com.jr.pojo.InformationInWhiteMess;
import com.jr.service.impl.SupplierBlackListServiceImpl;
import com.jr.util.WhitePageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/SupplierWhiteList/*")
public class SupplierWhiteListController extends HttpServlet {
    com.jr.service.impl.SupplierWhiteListImpl supplierWhiteList = new com.jr.service.impl.SupplierWhiteListImpl();
    SupplierBlackListServiceImpl supplierBlackListService = new SupplierBlackListServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset = utf-8");


        String url = request.getRequestURI();
        String methodName = url.substring(url.lastIndexOf("/") + 1);
        Class cla = SupplierWhiteListController.class;
        try {
            Method method = cla.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(cla.newInstance(), request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    //1.全查 带条件查
    public void query(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String account = request.getParameter("account");
        String name = request.getParameter("name");
        String level = request.getParameter("level");
        String pageindex = request.getParameter("pageindex");  WhitePageHelper whitePageHelper = new WhitePageHelper();
        if (pageindex != null && pageindex != ""){
            whitePageHelper.setIndexPage(Integer.parseInt(pageindex));
        }
        ArrayList<InformationInWhiteMess> list = supplierWhiteList.selectAllMess(account, name, level,whitePageHelper);
       whitePageHelper.setList(list);
       int i = supplierWhiteList.selectSize(account,level,name);
       whitePageHelper.setCountData(i);
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(whitePageHelper));
    }

    //2.改变企业水平
    public void changeLevel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String account = request.getParameter("account");
        String changeLevel = request.getParameter("changeLevel");
        String name = request.getParameter("name");
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(supplierWhiteList.updateSupplierWhiteList(changeLevel, account, name)));

    }

    //3.添加黑名单
    public void addSupplierBlackList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String account = request.getParameter("account");
        String reason = request.getParameter("reason");
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(supplierWhiteList.deleteSupplierWhiteList(account, name)));
        SupplierBlackListServiceImpl supplierBlackListService = new SupplierBlackListServiceImpl();
        supplierBlackListService.insertSupplierBlackList(account, name, reason);
    }

    //4.显示可供选择的企业名单
    public void addSupplierWhiteList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String account = request.getParameter("account");
        ArrayList<AddSupplierWhiteList> lists = supplierWhiteList.selectSupplier(account);
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(lists));
    }

    //5.添加白名单
    public void AddSupplierWhiteList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String account = request.getParameter("account");
        String level = request.getParameter("level");
        String empName = request.getParameter("empName");
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(supplierWhiteList.insertSupplierWhiteList(account, empName, level)));
    }
}
